const VodUploader = require('../../lib/vod-web-sdk-v5.1');
const app = getApp();

Page({

	data: {
		fileName: ''
	},
	getSignature: function(callback) {
		wx.request({
			url: app.apiDomain + 'index.php?c=user&a=getUploadSign',
			method: 'POST',
			dataType: 'json',
			success: function(res) {
				console.log(res);
				if (res.data && res.data.data) {
					callback(res.data.data);
				} else {
					return '获取签名失败';
				}
			}
		});
	},
	inputChange: function(evt) {
		this.fileName = evt.detail.value;
	},
	img_load: function(e) {
		this.img_i = 0;
		this.res_img = [];
		var thas = this;
		wx.chooseImage({
			count: 9,
			sizeType: ['original', 'compressed'],
			sourceType: ['album', 'camera'],
			success: function(res) {
				thas.img_tmp = res.tempFilePaths;
				// console.log(res.tempFilePaths)
				thas.img_quality(thas.img_tmp[thas.img_i])
				thas.setData({
					imgList: [],
					imgList2:[]
				});
			}
		})
	},
	img_quality: function(e) {
		// console.log(1)length
		var thas = this;
		// for (var i in e) {
			wx.compressImage({
				src: e, // 图片路径
				quality: 60, // 压缩质量
				success: function(res) {
					// var a = ''+res.tempFilePath;
					thas.res_img = res.tempFilePath;
					// console.log(thas.res_img)
					// if(e.length == thas.res_img.length){
						// console.log('a1')
						thas.img_up(thas.res_img,thas.img_i)
					// }
				}
			})
		// }
	},
	img_up:function(a,i){
		// console.log(a.length,i)
		if(this.img_tmp.length > i){
			// console.log('b1')
			var thas = this;
			wx.uploadFile({ //提交图片
				url: app.apiDomain + "index.php?c=group&a=uploadImage", //接口
				filePath:a,
				name: 'file',
				formData: {
					openid : wx.getStorageSync("openid")
				},
				dataType:'json',
				method: 'post',
				success: function(res) {
					// console.log('c1')
					var b = JSON.parse(res.data);
					thas.setData({
						imgList: thas.data.imgList.concat(b.data),
						imgList2: thas.data.imgList2.concat(a),
						img_get: 'off'
					});
					// console.log(thas.data.imgList,thas.data.imgList2)
					//
					thas.img_i = parseFloat(thas.img_i)+1;
					thas.img_quality(thas.img_tmp[thas.img_i],thas.img_i)
					//
				}
			});
		};
	},
	form_submit: function(e) {
		var thas = this;
		if (!this.data.img_get) {
			wx.showToast({
				title: '请上传图片',
				icon: 'none',
				duration: 1000
			});
			return false
		};
		if (!e.detail.value.textarea) {
			wx.showToast({
				title: '请填写内容',
				icon: 'none',
				duration: 1000
			});
			return false
		};
		if (!this.data.hua_ti_num) {
			wx.showToast({
				title: '请选择圈子',
				icon: 'none',
				duration: 1000
			});
			return false
		};
		wx.showLoading({
			title: '发表中',
		});
		// console.log(thas.data.imgList)
		//
		app.request(app.apiDomain + "index.php?c=group&a=addArticle", {
			content: e.detail.value.textarea,
			topic_id: thas.data.hua_ti_num,
			remark: '',
			imgs: thas.data.imgList.join(','),
		}, function(res) {
			// console.log(res);
			if(res.data.status==1){
				setTimeout(function() {
					wx.reLaunch({
						url: 'index'
					})
				}, 1200);
			}
			wx.showToast({
				title: res.data.info,
				icon: 'none',
				duration: 1000
			});

		});
	},
	hua_ti_click: function() {
		this.setData({
			hua_ti_on: 'on'
		})
	},
	hua_ti_off: function() {
		this.setData({
			hua_ti_on: ''
		})
	},
	hua_li_click: function(e) {
		// console.log(e)
		this.setData({
			hua_ti_on: '',
			hua_ti_wrds: e.currentTarget.dataset.wrds,
			hua_ti_num: e.currentTarget.dataset.id
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var thas = this;
		app.request(app.apiDomain + "index.php?c=group&a=topic", {}, function(res) {
			// console.log(res.data.data)
			thas.setData({
				topic: res.data.data
			});
		});

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		return app.onShareAppMessage();
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
